#pragma  once
#include <WinSock2.h>
#include <vector>
#include <map>
#include <memory>
#include <string>
#include <deque>
#include "base.h"
#include "NetBase.h"


class NetServer;
class NetConnection;

class NetServer
{
public:
	enum
	{
		State_None,
		State_Normal,
		State_Stopping,
		State_Stopped,
	};

	NetServer(Work_Func _func,ServerID id,const char* pIP,int nPort);
	NetServer(Accepted_Handler _func,ServerID id,const char* pIP,int nPort);

	bool	 Start(HANDLE IOCP);
	HANDLE   GetExitEvent() const   {return m_exitEvent;}
	ServerID GetServerID()const		{return m_id;}
	int      GetConnectionCount(); 
	bool     AddConnection(ConectionID nConID,HANDLE IOCP);
	void	 RemoveConnection(NetConnection* pCon);
	NetConnection* GetConnection(ConectionID id)const;

	void     Stop();
	void     Stopping();
	void     CleanUp();

	typedef std::map<ConectionID/*ConnectionID*/,NetConnection*> MAP_CONNECTION;
	typedef MAP_CONNECTION::iterator MAP_CONNECTION_ITER;

private:
	ServerID		m_id;
	Work_Func		m_workFunc;
	Accepted_Handler m_AccpetHandler;

	SOCKET			m_listenSocket;
	HANDLE			m_exitEvent;
	const char*		m_pIP;
	int				m_nPort;

	MAP_CONNECTION  m_mapConnection;
	My_CriticalSection m_ConnectionCritical;

	int				m_state;
};

